package com.css.fxfzypg.modules.baseData.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.modules.baseData.queryParams.EarthquakeDisasterQueryParams;
import com.css.fxfzypg.modules.baseData.entity.EarthquakeDisasterEntity;
import com.css.fxfzypg.modules.baseData.service.EarthquakeDisasterService;
import com.css.fxfzypg.modules.baseData.vo.EarthquakeDisasterVO;
import com.css.fxfzypg.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author y1fan
 * @create 2022-08-17 17:00
 * 历史地震灾害数据子模块
 */
@RestController
@RequestMapping("/fxfzypg/baseData")
@Slf4j
public class EarthquakeDisasterController {

    @Resource
    private EarthquakeDisasterService earthquakeDisasterService;

    @PostMapping("/saveEarthquakeDisaster")
    public RestResponse saveEarthquakeDisaster(@RequestBody EarthquakeDisasterEntity earthquakeDisasterEntity){
        RestResponse restResponse = null;
        try {
            Boolean boo = earthquakeDisasterService.saveEarthquakeDisaster(earthquakeDisasterEntity);
            if(boo){
                restResponse = RestResponse.succeed("新增成功");
            }else{
                restResponse = RestResponse.succeed("新增失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            String errormessage = "新增失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    @GetMapping("/getEarthquakeDisasterId/{id}")
    public RestResponse getEarthquakeDisasterById(@PathVariable("id")String id){
        RestResponse restResponse = null;
        try {
            EarthquakeDisasterVO earthquakeDisasterVO = earthquakeDisasterService.getEarthquakeDisasterById(id);

            restResponse = RestResponse.succeed(earthquakeDisasterVO);
        } catch (Exception e) {
            e.printStackTrace();
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    @GetMapping("/getAllEarthquakeDisaster")
    public RestResponse<JSONObject> getAllEarthquakeDisaster(HttpServletRequest request, EarthquakeDisasterQueryParams queryParams){
        RestResponse restResponse = null;
        try{
              int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
               int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));

            queryParams.setCurPage(curPage);
            queryParams.setPageSize(pageSize);
            Map<String,Object> map = earthquakeDisasterService.getAllEarthquakeDisasterByParams(queryParams);
            restResponse = RestResponse.succeed(map);
        }catch (Exception e){
            e.printStackTrace();
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }
     @PostMapping("/updateEarthquakeDisaster")
     public RestResponse updateEarthquakeDisaster(@RequestBody EarthquakeDisasterEntity earthquakeDisasterEntity){
        RestResponse restResponse = null;
        try {
            Boolean boo = earthquakeDisasterService.updateEarthquakeDisaster(earthquakeDisasterEntity);
            if(boo){
                restResponse = RestResponse.succeed("修改成功");
            }else{
                restResponse = RestResponse.succeed("修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            String errormessage = "修改失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
         return restResponse;
     }


    @PostMapping("/deleteEarthquakeDisaster/{ids}")
    public RestResponse deleteEarthquakeDisaster(@PathVariable("ids") List<String> ids){
        RestResponse restResponse = null;
        try {
            Boolean boo = earthquakeDisasterService.deleteEarthquakeDisaster(ids);
            if(boo){
                restResponse = RestResponse.succeed("删除成功");
            }else{
                restResponse = RestResponse.succeed("删除失败");
            }

        } catch (Exception e) {
            e.printStackTrace();
            String errormessage = "修改失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 导出
     * @param queryParams
     * @return
     */
    @PostMapping("/earthquakeDisasterExportExcel")
    public RestResponse exportExcel(HttpServletResponse response,EarthquakeDisasterQueryParams queryParams) {
        RestResponse restResponse = null;
        try {
            earthquakeDisasterService.exportExcel(response,queryParams);
            restResponse = RestResponse.succeed("导出成功");
        } catch (Exception e) {
            String errormessage = e.getMessage();
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }



    /**
     * 导入
     * @param file
     * @return
     */
    @PostMapping("/earthquakeDisasterImportExcel")
    public RestResponse importExcel(MultipartFile file){
        RestResponse response = null;
        try {
            response = earthquakeDisasterService.importExcel(file);
            response.setMessage((String) response.getData());
        } catch (Exception e) {
            String errormessage = "导入失败";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }


}
